import axios from "axios";
import store from "@/store";
import {Message, MessageBox, Loading} from "element-ui";
import {getToken, removeToken} from '@/util/auth'
import {baseUrl} from "@/config/env";

let loadingInstance = "";

const HttpConnection = axios.create({
  baseURL: baseUrl
});

HttpConnection.defaults.headers.post["Content-Type"] = "application/json";

HttpConnection.interceptors.request.use(
  config => {
    config.headers["Authorization"] = 'Bearer ' + getToken();
    loadingInstance = Loading.service(
      {
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      }
    );
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

HttpConnection.interceptors.response.use(
  response => {
    loadingInstance.close();
    if (response.data.code === 501) {
      MessageBox.alert("系统未登录，请重新登录", "错误", {
        confirmButtonText: "确定",
        type: "error"
      }).then(() => {
        store.dispatch("FedLogOut").then(() => {
          location.reload();
        });
      });
      return Promise.reject("error");
    } else if (response.data.code === 502) {
      MessageBox.alert("系统内部错误，请联系管理员维护", "错误", {
        confirmButtonText: "确定",
        type: "error"
      });
      return Promise.reject("error");
    } else if (response.data.code === 503) {
      MessageBox.alert("请求业务目前未支持", "警告", {
        confirmButtonText: "确定",
        type: "error"
      });
      return Promise.reject("error");
    } else if (response.data.code === 504) {
      MessageBox.alert("更新数据已经失效，请刷新页面重新操作", "警告", {
        confirmButtonText: "确定",
        type: "error"
      });
      return Promise.reject("error");
    } else if (response.data.code === 505) {
      MessageBox.alert("更新失败，请再尝试一次", "警告", {
        confirmButtonText: "确定",
        type: "error"
      });
      return Promise.reject("error");
    } else if (response.data.code === 506) {
      MessageBox.alert("没有操作权限，请联系管理员授权", "错误", {
        confirmButtonText: "确定",
        type: "error"
      });
      return Promise.reject("error");
    } else if (response.data.code === 6004) {
      Message({
        message: response.data.msg,
        type: "error",
        duration: 5 * 1000
      });
      removeToken();
      localStorage.clear();
      location.reload();
      return Promise.reject("error");
    } else if (response.data.code === 200) {
      return response.data;
    } else if (response.data.code !== 0) {
      // 非5xx的错误属于业务错误，留给具体页面处理
      Message({
        message: response.data.msg,
        type: "error"
      });
      return Promise.reject(response.data);
    }
  },
  error => {
    loadingInstance.close();
    Message({
      message: "登录连接超时（后台不能连接，请联系系统管理员）",
      type: "error",
      duration: 5 * 1000
    });
    return Promise.reject(error);
  }
);

export default HttpConnection;

/*
测试请求

HttpConnection.get("/user", {
    params: {}
}).then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});

HttpConnection.post("/user", {
    firstName: "",
    lastName: ""
}).then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});

Vue.prototype.$message({
    message: response.data.error_msg,
    type: "error"
});
*/
